package com.atguigu.serviceedu.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.clients.OrderClient;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.entity.vo.front.CourseQueryVo;
import com.atguigu.serviceedu.entity.vo.front.CourseWebVo;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Classname CourseFrontController
 * @Description TODO
 * @Date 2022/9/14 14:46
 * @Created by ZHL17
 */
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @PostMapping("getCourseFront/{page}/{limit}")
    public R getCourseFront(@PathVariable long page, @PathVariable long limit,@RequestBody CourseQueryVo courseQueryVo){
        Page<EduCourse> coursePage=new Page<>(page,limit);
        Map<String,Object> map=courseService.getCoursePage(coursePage,courseQueryVo);
        return R.ok().data(map);
    }
    @ApiOperation(value = "根据ID查询课程")
    @GetMapping( "selectCourseInfo/{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getAllChaptersByCourseId(courseId);

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuy=false;
        if(memberId!=null){
            isBuy = orderClient.isBuyCourse(memberId, courseId);
        }

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isBuy",isBuy);
    }


}
